import React, { Component } from 'react'
import { Container, Content } from 'native-base'
import { View, TouchableOpacity, Text, DeviceEventEmitter } from 'react-native'
import DialogCom from '../dialog'
import TipsCom from '../dialog/tips'
import { getAwardOneApi } from './data'
import { connect } from 'react-redux' // 引入connect函数

class Commoncontainer extends Component {
	constructor(props) {
		super(props)
		this.state = {
			dialogVisible: false,
			dialogContent: '',
			dialogTitle: '',
			
			dialogForbidVisible: false,
			dialogForbidContent: '',
			dialogForbidTitle: '封号处罚',
			
			tipsVisible: false,
			tipsContent: ''
		}
	}
	componentWillMount() {
		DeviceEventEmitter.addListener('forbid.user', () => {
			if (this.state.dialogForbidVisible) return
			this.setState({
				dialogForbidVisible: true,
				dialogForbidContent: 
					'很抱歉，经系统检测您的账号存在违规行为，现已封号处理，如有疑问请联系客服！微信客服: ' + this.props.serviceInfo.wechat
			})
		})
		DeviceEventEmitter.addListener('login.success', () => {
			this.getAward()
		})
		DeviceEventEmitter.addListener('show.tips', (text) => {
			this.showTips(text)
		})
		this.getAward()
	}
	async getAward () {
		let res = await getAwardOneApi()
		if (res && !this.state.dialogVisible) {
			this.setState({
				dialogVisible: true,
				dialogContent: `恭喜你获得每日登录奖励${res}元`,
				dialogTitle: '每日登录奖励'
			})
		}
	}
	showDialog(text) {
		this.setState({
			dialogVisible: true,
			dialogContent: text
		})
	}
	showTips(text) {
		this.setState({
			tipsVisible: true,
			tipsContent: text
		})
		setTimeout(() => {
			this.setState({
				tipsVisible: false
			})
		}, 1000)
	}
	render() {
		return (
			<Container>
				<Content style={{ backgroundColor: '#f5f5f5' }}>
					{this.props.children}
					<DialogCom 
						onClose={() => {
							this.setState({
								dialogVisible: false
							})
						}}
						titleStyle={
							{
								textStyle: {
									color: '#C30D23'
								}
							}
						}
						title={this.state.dialogTitle}
						content={this.state.dialogContent}
						visible={this.state.dialogVisible} />
					
					<DialogCom 
						titleStyle={
							{
								textStyle: {
									color: '#C30D23'
								}
							}
						}
						notOk={true}
						title={this.state.dialogForbidTitle}
						content={this.state.dialogForbidContent}
						visible={this.state.dialogForbidVisible} />
						
					<TipsCom 
						onClose={() => {
							this.setState({
								tipsVisible: false
							})
						}}
						visible={this.state.tipsVisible} 
						content={this.state.tipsContent} />
				</Content>
			</Container>
		)
	}
}

export default connect(
	state => ({
		userinfo: state.user.user || {},
		serviceInfo: state.login.serviceInfo
	})
)(Commoncontainer)
